﻿using Microsoft.EntityFrameworkCore;

namespace 多对多关系_双向导航
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                //Student student1 = new Student() { Name = "Test" };
                //Student student2 = new Student() { Name = "Test1" };
                //Student student3 = new Student() { Name = "Test3" };
                //Student student4 = new Student() { Name = "Test4" };

                //Teacher teacher1 = new Teacher() { Name = "ls1" };
                //Teacher teacher2 = new Teacher() { Name = "ls2" };

                //teacher1.Students.Add(student1);

                //teacher2.Students.Add(student2);
                //teacher2.Students.Add(student1);

                //teacher2.Students.Add(student2);
                //teacher2.Students.Add(student1);
                //teacher2.Students.Add(student3);
                //teacher2.Students.Add(student4);

                //ctx.Students.Add(student1);
                //ctx.Students.Add(student2);
                //ctx.Students.Add(student3);  
                //ctx.Students.Add(student4);

                //ctx.Teachers.Add(teacher1);
                //ctx.Teachers.Add(teacher2);


                //ctx.SaveChanges();

                var teachers = ctx.Teachers.Include(t => t.Students);
                foreach (var item in teachers)
                {
                    Console.WriteLine(item.Name);
                    foreach (var student in item.Students)
                    {
                        Console.WriteLine($"-------------{student.Name}");
                    }
                }
            }
        }
    }
}
